No Starch Press Ruby Wizardry, An Introduction to Programming for Kids (2015)

No Starch Press Ruby Wizardry, An Introduction to Programming for Kids (2015)

Author:unknow
Format: epub
Published: 0101-01-01T00:00:00+00:00


Object-Oriented Programming

Finally, we learned that writing programs that revolve around classes and objects is called object-oriented programming (OOP). Our minstrel is a good example of an object: a piece of code that acts just like something in the real world! It has attributes (facts about itself) as well as behavior, which is just a way of talking about the methods the object knows how to use. We can define the behavior of any minstrel with a Minstrel class, as follows.

minstrel_review_3.rb

class Minstrel attr_reader :name @@number_of_minstrels = 0 def initialize(name) @name = name @@number_of_minstrels += 1 end def sing(song_name) puts "Time to sing a song called: #{song_name}!" puts 'Tralala!' end def self.number_of_minstrels @@number_of_minstrels end end

Our class has an attr_reader for :name (meaning we can read the name, but not change it), as well as a @@number_of_minstrels class variable that keeps track of how many instances we’ve created and an initialize method that gives our minstrel a name and increases the @@number_of_minstrels.

It also has two methods: one, sing, is a method of minstrel instances and sings a little song, and the other, self.number_of_minstrels, is a method of the Minstrel class and tells us how many minstrels we’ve created so far.

Let’s see them in action!

>> load 'minstrel_review_3.rb' => true >> wherefore = Minstrel.new('Wherefore') => #<Minstrel:0x000001031eac68 @name="Wherefore"> >> Minstrel.number_of_minstrels => 1 wherefore.sing('A Tail of Two Foxes') Time to sing a song called: A Tail of Two Foxes! Tralala! => nil

Voilà! We can create a new minstrel, call Minstrel.number_of_minstrels to see that we’ve created one, and then call our minstrel instance (wherefore)’s sing method to hear his ballad “A Tail of Two Foxes.”

Things are starting to get a bit suspenseful around here, so I’m gonna go grab a bag of popcorn—be right with you. In the meantime, go on ahead to see what the King, Scarlet, and Ruben find when they get back to the castle, and get ready for even more object-oriented Ruby magic!



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(73918)
What's Done in Darkness by Kayla Perrin(26959)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20859)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20612)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20537)
The Fifty Shades Trilogy & Grey by E L James(19456)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19381)
Shot Through the Heart by Mercy Celeste(19242)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17387)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17356)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17186)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(17099)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16713)
The Subtle Art of Not Giving a F*ck by Mark Manson(14830)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14443)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(14219)
Cozy crochet hats: 7 Stylish and Beginner-Friendly Patterns from Baby Beanies to Trendy Bucket Hats by Vanilla Lazy(13504)
Scorched Earth by Nick Kyme(13096)
Reichel W. Numerical methods for Electrical Engineering, Meteorology,...2022 by Unknown(12980)
Drei Generationen auf dem Jakobsweg by Stein Pia(11259)